<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="QR,Java Code," />










<meta name="description" content="生成带背景图片的二维码">
<meta property="og:type" content="article">
<meta property="og:title" content="生成二维码">
<meta property="og:url" content="http://wanazi.oschina.io/2016/11/16/生成二维码/index.html">
<meta property="og:site_name" content="wanazi">
<meta property="og:description" content="生成带背景图片的二维码">
<meta property="og:image" content="http://wanazi.oschina.io/2016/11/16/生成二维码/logo.jpg">
<meta property="og:image" content="http://wanazi.oschina.io/2016/11/16/生成二维码/out.jpg">
<meta property="og:updated_time" content="2019-02-27T12:20:15.831Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="生成二维码">
<meta name="twitter:description" content="生成带背景图片的二维码">
<meta name="twitter:image" content="http://wanazi.oschina.io/2016/11/16/生成二维码/logo.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://wanazi.oschina.io/2016/11/16/生成二维码/"/>





  <title>生成二维码 | wanazi</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">wanazi</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">偶尔记一些东西</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://wanazi.oschina.io/2016/11/16/生成二维码/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="tqd">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="wanazi">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">生成二维码</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-11-16T10:56:42+08:00">
                2016-11-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          
              <div class="post-description">
                  生成带背景图片的二维码
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> com.tqd.test;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.awt.Color;</div><div class="line"><span class="keyword">import</span> java.awt.Desktop;</div><div class="line"><span class="keyword">import</span> java.awt.Graphics2D;</div><div class="line"><span class="keyword">import</span> java.awt.Image;</div><div class="line"><span class="keyword">import</span> java.awt.geom.AffineTransform;</div><div class="line"><span class="keyword">import</span> java.awt.image.AffineTransformOp;</div><div class="line"><span class="keyword">import</span> java.awt.image.BufferedImage;</div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"><span class="keyword">import</span> java.io.IOException;</div><div class="line"><span class="keyword">import</span> java.util.HashMap;</div><div class="line"><span class="keyword">import</span> java.util.Map;</div><div class="line"></div><div class="line"><span class="keyword">import</span> javax.imageio.ImageIO;</div><div class="line"></div><div class="line"><span class="keyword">import</span> com.google.zxing.BarcodeFormat;</div><div class="line"><span class="keyword">import</span> com.google.zxing.EncodeHintType;</div><div class="line"><span class="keyword">import</span> com.google.zxing.MultiFormatWriter;</div><div class="line"><span class="keyword">import</span> com.google.zxing.WriterException;</div><div class="line"><span class="keyword">import</span> com.google.zxing.common.BitMatrix;</div><div class="line"><span class="keyword">import</span> com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Main</span> </span>{</div><div class="line">    <span class="comment">// &#x56FE;&#x7247;&#x5BBD;&#x5EA6;&#x7684;&#x4E00;&#x822C;</span></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> IMAGE_WIDTH = <span class="number">120</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> IMAGE_HEIGHT = <span class="number">120</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> IMAGE_HALF_WIDTH = IMAGE_WIDTH / <span class="number">2</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> FRAME_WIDTH = <span class="number">2</span>;</div><div class="line">    <span class="comment">// &#x4E8C;&#x7EF4;&#x7801;&#x5199;&#x7801;&#x5668;</span></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> MultiFormatWriter mutiWriter = <span class="keyword">new</span> MultiFormatWriter();</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">encode</span><span class="params">(String content, <span class="keyword">int</span> width, <span class="keyword">int</span> height, String srcImagePath, String destImagePath)</span> </span>{</div><div class="line">        <span class="keyword">try</span> {</div><div class="line">            ImageIO.write(genBarcode(content, width, height, srcImagePath), <span class="string">&quot;jpg&quot;</span>, <span class="keyword">new</span> File(destImagePath));</div><div class="line">        } <span class="keyword">catch</span> (IOException e) {</div><div class="line">            e.printStackTrace();</div><div class="line">        } <span class="keyword">catch</span> (WriterException e) {</div><div class="line">            e.printStackTrace();</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> BufferedImage <span class="title">genBarcode</span><span class="params">(String content, <span class="keyword">int</span> width, <span class="keyword">int</span> height, String srcImagePath)</span></span></div><div class="line">            <span class="keyword">throws</span> WriterException, IOException {</div><div class="line">        <span class="comment">// &#x8BFB;&#x53D6;&#x6E90;&#x56FE;&#x50CF;</span></div><div class="line">        BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH, IMAGE_HEIGHT, <span class="keyword">true</span>);</div><div class="line">        <span class="keyword">int</span>[][] srcPixels = <span class="keyword">new</span> <span class="keyword">int</span>[IMAGE_WIDTH][IMAGE_HEIGHT];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; scaleImage.getWidth(); i++) {</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; scaleImage.getHeight(); j++) {</div><div class="line">                srcPixels[i][j] = scaleImage.getRGB(i, j);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        Map&lt;EncodeHintType, Object&gt; hint = <span class="keyword">new</span> HashMap&lt;EncodeHintType, Object&gt;();</div><div class="line">        hint.put(EncodeHintType.CHARACTER_SET, <span class="string">&quot;utf-8&quot;</span>);</div><div class="line">        hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);</div><div class="line">        <span class="comment">// &#x751F;&#x6210;&#x4E8C;&#x7EF4;&#x7801;</span></div><div class="line">        BitMatrix matrix = mutiWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hint);</div><div class="line">        <span class="comment">// &#x4E8C;&#x7EF4;&#x77E9;&#x9635;&#x8F6C;&#x4E3A;&#x4E00;&#x7EF4;&#x50CF;&#x7D20;&#x6570;&#x7EC4;</span></div><div class="line">        <span class="keyword">int</span> halfW = matrix.getWidth() / <span class="number">2</span>;</div><div class="line">        <span class="keyword">int</span> halfH = matrix.getHeight() / <span class="number">2</span>;</div><div class="line">        <span class="keyword">int</span>[] pixels = <span class="keyword">new</span> <span class="keyword">int</span>[width * height];</div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> y = <span class="number">0</span>; y &lt; matrix.getHeight(); y++) {</div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> x = <span class="number">0</span>; x &lt; matrix.getWidth(); x++) {</div><div class="line">                <span class="comment">// &#x8BFB;&#x53D6;&#x56FE;&#x7247;</span></div><div class="line">                <span class="keyword">if</span> (x &gt; halfW - IMAGE_HALF_WIDTH &amp;&amp; x &lt; halfW + IMAGE_HALF_WIDTH &amp;&amp; y &gt; halfH - IMAGE_HALF_WIDTH</div><div class="line">                        &amp;&amp; y &lt; halfH + IMAGE_HALF_WIDTH) {</div><div class="line">                    pixels[y * width + x] = srcPixels[x - halfW + IMAGE_HALF_WIDTH][y - halfH + IMAGE_HALF_WIDTH];</div><div class="line">                }</div><div class="line">                <span class="comment">// &#x5728;&#x56FE;&#x7247;&#x56DB;&#x5468;&#x5F62;&#x6210;&#x8FB9;&#x6846;</span></div><div class="line">                <span class="keyword">else</span> <span class="keyword">if</span> ((x &gt; halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH &amp;&amp; x &lt; halfW - IMAGE_HALF_WIDTH + FRAME_WIDTH</div><div class="line">                        &amp;&amp; y &gt; halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH &amp;&amp; y &lt; halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)</div><div class="line">                        || (x &gt; halfW + IMAGE_HALF_WIDTH - FRAME_WIDTH &amp;&amp; x &lt; halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH</div><div class="line">                                &amp;&amp; y &gt; halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH</div><div class="line">                                &amp;&amp; y &lt; halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)</div><div class="line">                        || (x &gt; halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH &amp;&amp; x &lt; halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH</div><div class="line">                                &amp;&amp; y &gt; halfH - IMAGE_HALF_WIDTH - FRAME_WIDTH</div><div class="line">                                &amp;&amp; y &lt; halfH - IMAGE_HALF_WIDTH + FRAME_WIDTH)</div><div class="line">                        || (x &gt; halfW - IMAGE_HALF_WIDTH - FRAME_WIDTH &amp;&amp; x &lt; halfW + IMAGE_HALF_WIDTH + FRAME_WIDTH</div><div class="line">                                &amp;&amp; y &gt; halfH + IMAGE_HALF_WIDTH - FRAME_WIDTH</div><div class="line">                                &amp;&amp; y &lt; halfH + IMAGE_HALF_WIDTH + FRAME_WIDTH)) {</div><div class="line">                    pixels[y * width + x] = <span class="number">0xfffffff</span>;</div><div class="line">                } <span class="keyword">else</span> {</div><div class="line">                    <span class="comment">// &#x6B64;&#x5904;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x4E8C;&#x7EF4;&#x7801;&#x7684;&#x989C;&#x8272;&#xFF0C;&#x53EF;&#x4EE5;&#x5206;&#x522B;&#x5236;&#x5B9A;&#x4E8C;&#x7EF4;&#x7801;&#x548C;&#x80CC;&#x666F;&#x7684;&#x989C;&#x8272;&#xFF1B;</span></div><div class="line">                    pixels[y * width + x] = matrix.get(x, y) ? <span class="number">0xff000000</span> : <span class="number">0xfffffff</span>;</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">        BufferedImage image = <span class="keyword">new</span> BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);</div><div class="line">        image.getRaster().setDataElements(<span class="number">0</span>, <span class="number">0</span>, width, height, pixels);</div><div class="line">        <span class="keyword">return</span> image;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">/**</span></div><div class="line">     * &#x628A;&#x4F20;&#x5165;&#x7684;&#x539F;&#x59CB;&#x56FE;&#x50CF;&#x6309;&#x9AD8;&#x5EA6;&#x548C;&#x5BBD;&#x5EA6;&#x8FDB;&#x884C;&#x7F29;&#x653E;&#xFF0C;&#x751F;&#x6210;&#x7B26;&#x5408;&#x8981;&#x6C42;&#x7684;&#x56FE;&#x6807;</div><div class="line">     * </div><div class="line">     * <span class="doctag">@param</span> srcImageFile</div><div class="line">     *            &#x6E90;&#x6587;&#x4EF6;&#x5730;&#x5740;</div><div class="line">     * <span class="doctag">@param</span> height</div><div class="line">     *            &#x76EE;&#x6807;&#x9AD8;&#x5EA6;</div><div class="line">     * <span class="doctag">@param</span> width</div><div class="line">     *            &#x76EE;&#x6807;&#x5BBD;&#x5EA6;</div><div class="line">     * <span class="doctag">@param</span> hasFiller</div><div class="line">     *            &#x6BD4;&#x4F8B;&#x4E0D;&#x5BF9;&#x65F6;&#x662F;&#x5426;&#x9700;&#x8981;&#x8865;&#x767D;&#xFF1A;true&#x4E3A;&#x8865;&#x767D;; false&#x4E3A;&#x4E0D;&#x8865;&#x767D;;</div><div class="line">     * <span class="doctag">@throws</span> IOException</div><div class="line">     */</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> BufferedImage <span class="title">scale</span><span class="params">(String srcImageFile, <span class="keyword">int</span> height, <span class="keyword">int</span> width, <span class="keyword">boolean</span> hasFiller)</span></span></div><div class="line">            <span class="keyword">throws</span> IOException {</div><div class="line">        <span class="keyword">double</span> ratio = <span class="number">0.0</span>; <span class="comment">// &#x7F29;&#x653E;&#x6BD4;&#x4F8B;</span></div><div class="line">        File file = <span class="keyword">new</span> File(srcImageFile);</div><div class="line">        BufferedImage srcImage = ImageIO.read(file);</div><div class="line">        Image destImage = srcImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);</div><div class="line">        <span class="comment">// &#x8BA1;&#x7B97;&#x6BD4;&#x4F8B;</span></div><div class="line">        <span class="keyword">if</span> ((srcImage.getHeight() &gt; height) || (srcImage.getWidth() &gt; width)) {</div><div class="line">            <span class="keyword">if</span> (srcImage.getHeight() &gt; srcImage.getWidth()) {</div><div class="line">                ratio = (<span class="keyword">new</span> Integer(height)).doubleValue() / srcImage.getHeight();</div><div class="line">            } <span class="keyword">else</span> {</div><div class="line">                ratio = (<span class="keyword">new</span> Integer(width)).doubleValue() / srcImage.getWidth();</div><div class="line">            }</div><div class="line">            AffineTransformOp op = <span class="keyword">new</span> AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), <span class="keyword">null</span>);</div><div class="line">            destImage = op.filter(srcImage, <span class="keyword">null</span>);</div><div class="line">        }</div><div class="line">        <span class="keyword">if</span> (hasFiller) {<span class="comment">// &#x8865;&#x767D;</span></div><div class="line">            BufferedImage image = <span class="keyword">new</span> BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);</div><div class="line">            Graphics2D graphic = image.createGraphics();</div><div class="line">            graphic.setColor(Color.white);</div><div class="line">            graphic.fillRect(<span class="number">0</span>, <span class="number">0</span>, width, height);</div><div class="line">            <span class="keyword">if</span> (width == destImage.getWidth(<span class="keyword">null</span>))</div><div class="line">                graphic.drawImage(destImage, <span class="number">0</span>, (height - destImage.getHeight(<span class="keyword">null</span>)) / <span class="number">2</span>, destImage.getWidth(<span class="keyword">null</span>),</div><div class="line">                        destImage.getHeight(<span class="keyword">null</span>), Color.white, <span class="keyword">null</span>);</div><div class="line">            <span class="keyword">else</span></div><div class="line">                graphic.drawImage(destImage, (width - destImage.getWidth(<span class="keyword">null</span>)) / <span class="number">2</span>, <span class="number">0</span>, destImage.getWidth(<span class="keyword">null</span>),</div><div class="line">                        destImage.getHeight(<span class="keyword">null</span>), Color.white, <span class="keyword">null</span>);</div><div class="line">            graphic.dispose();</div><div class="line">            destImage = image;</div><div class="line">        }</div><div class="line">        <span class="keyword">return</span> (BufferedImage) destImage;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</div><div class="line">        Main.encode(<span class="string">&quot;12345678&quot;</span>, <span class="number">300</span>, <span class="number">300</span>, <span class="string">&quot;./data/logo.jpg&quot;</span>, <span class="string">&quot;./data/out.jpg&quot;</span>);</div><div class="line">        </div><div class="line">        <span class="keyword">try</span> {</div><div class="line">            Desktop.getDesktop().open(<span class="keyword">new</span> File(<span class="string">&quot;./data/out.jpg&quot;</span>));</div><div class="line">        } <span class="keyword">catch</span> (IOException e) {</div><div class="line">            <span class="comment">// TODO Auto-generated catch block</span></div><div class="line">            e.printStackTrace();</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div></pre></td></tr></table></figure>
<p><img src="/2016/11/16/&#x751F;&#x6210;&#x4E8C;&#x7EF4;&#x7801;/logo.jpg" alt="logo&#x6587;&#x4EF6;"></p>
<p><img src="/2016/11/16/&#x751F;&#x6210;&#x4E8C;&#x7EF4;&#x7801;/out.jpg" alt="&#x8F93;&#x51FA;"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/QR/" rel="tag"># QR</a>
          
            <a href="/tags/Java-Code/" rel="tag"># Java Code</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/10/TestImage/" rel="next" title="TestImage">
                <i class="fa fa-chevron-left"></i> TestImage
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/11/18/Window10安装Xming/" rel="prev" title="Window10安装Xming">
                Window10安装Xming <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">tqd</p>
              <p class="site-description motion-element" itemprop="description">只是为了好玩</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tqd</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
